package data.admin;

import dao.SQLiteConnection;
import gui.admin.RegisterWin;
import javax.swing.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;

import static utils.AESEncryption.encrypt;

public class Register extends RegisterWin {
    Register(){
        getB1().addActionListener(e->{
            //获取文本框的内容
            String id= getT_id().getText();
            String name = getT_name().getText();
            String phone = getT_phone().getText();
            //获取下拉框内容
            String sex = (String) getT2().getSelectedItem();
            //获取密码框的内容
            char[] password = getT4().getPassword();
            String passwordStr = new String(password);
            // 使用 Arrays.fill() 方法清空 char 数组
            Arrays.fill(password, '\0');
            //空判断
            if (id == null||id.trim().isEmpty()) {
                JOptionPane.showMessageDialog(this,"身份证号不能为空！");
            }
            else if (name == null||name.trim().isEmpty()) {
                JOptionPane.showMessageDialog(this,"姓名不能为空！");
            }
            else if (sex == null||sex.trim().isEmpty()) {
                JOptionPane.showMessageDialog(this,"性别不能为空！");
            }
            else if (phone == null||phone.trim().isEmpty()) {
                JOptionPane.showMessageDialog(this,"电话不能为空！");
            }
            else if (passwordStr.trim().isEmpty()) {
                JOptionPane.showMessageDialog(this,"密码不能为空！");
            }
            else if(!confirm(getT4(),getT5())){
                JOptionPane.showMessageDialog(this,"两次密码不一致！");
            }
            //存储注册信息
            else {
                //连接数据库
                SQLiteConnection Connection = new SQLiteConnection();
                Connection.Connect_SQLite();
                try {
                    // 密码加密
                    try {
                        passwordStr=encrypt(passwordStr);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    //
                    Connection conn = Connection.getConnection();
                    Statement stmt = conn.createStatement();
                    String sql1 = "INSERT INTO Reader (rid, rpassword, rname, sex, phone) " +
                            "VALUES ('"+id+"','"+ passwordStr + "','" + name + "', '" + sex + "', " + phone + ")";
                    stmt.executeUpdate(sql1);
                    System.out.println("数据已成功写入数据库");
                    JOptionPane.showMessageDialog(this, "注册成功");
                } catch (SQLException ex) {
                    System.out.println("写入数据库时发生错误：" + ex.getMessage());
                }
                //显示账号
//                try {
//                    Connection conn = Connection.getConnection();
//                    Statement stmt = conn.createStatement();
//                    String sql2="SELECT MAX(rid) FROM Reader WHERE rname='"+ name+ "'" + "AND phone="+phone;
//                    ResultSet rs = stmt.executeQuery(sql2);
//                    int rid = -1;
//                    int rx=-1;          //判断注册是否完成
//                    if (rs.next()) {
//                        rid = rs.getInt(1);
//                    }
//                    if(rid!=-1){
//                        System.out.println("账号查询成功");
//                        JOptionPane.showMessageDialog(this, "你的账号：" + rid);
//                        rx=1;   //显示账号，注册完成，置1
//                    }
//                    if(rx==1){
//                        dispose();
//                    }
//                } catch (SQLException ex) {
//                    System.out.println(ex.getMessage());
//                }
//                Connection.closeConnection();
            }
        });
        getB2().addActionListener(e-> dispose());
    }

    //确认两次密码是否一致
    boolean confirm(JPasswordField p1, JPasswordField p2){
        //获取密码
        char[] password1 = p1.getPassword();
        char[] password2 = p2.getPassword();
        //匹配密码
        boolean passwordsMatch = Arrays.equals(password1, password2);
        // 清空数组
        Arrays.fill(password1, ' ');
        Arrays.fill(password2, ' ');
        return passwordsMatch;
    }

}
